<template>
<view>
  <my-search @click='goSearch'></my-search>
  <view class="scroll-view-container">
    <!-- 左侧滚动区域 -->
    <view class="scroll-view-left">
      <scroll-view  scroll-y :style="{height:wh + 'px'}">
          <block v-for="(item,i) in cateList" :key="i"  >
            <view :class="['left-item',active === i ? 'active' : '' ]" @click='changeActive(i)'>
              {{item.cat_name}}
            </view>
          </block>
      </scroll-view>
    </view>
    <!-- 右侧滚动区域 -->
  	<view class="scroll-view-right">
      <scroll-view scroll-y :style="{height: wh + 'px'}" :scroll-top="scrollTop">
  
        <view v-for="(item2,i2) in cateLevel2" :key='i2' class="cate-lv2-item">
          <view class="cate-lv2-title">
            / {{item2.cat_name}} /
          </view>
          <!-- 动态渲染三级分类列表 -->
          <view class="cate-lv3-list">
            <view class="cate-lv3-item" v-for="(item3,i3) in item2.children" @click="goodsListTo(item3)">
              <image :src="item3.cat_icon"></image>
              <text> {{item3.cat_name}} </text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</view>
</template>

<script>
	export default {
		data() {
			return {
				wh:0,
        cateList:[],
        active:0,
        cateLevel2:[],
        scrollTop:0
			};
		},
    onLoad() {
      const info = uni.getSystemInfoSync()
      this.wh = info.windowHeight - 50
      this.getCateList()
    },
    methods:{
      async getCateList(){
        const {data : res} = await uni.$http.get('/categories')
        if(res.meta.status !== 200) return uni.$showMsg()
        this.cateList = res.message
        this.cateLevel2 = res.message[0].children
      },
      
      changeActive(i){
        this.active = i
        this.cateLevel2 = this.cateList[i].children
        this.scrollTop = this.scrollTop === 0 ? 1 : 0
      },
      
      goodsListTo(item3){
        uni.navigateTo({
          url:'/subpkg/goods_list/goods_list?cid=' + item3.cat_id
        })
      },
      goSearch(){
        uni.navigateTo({
          url:'/subpkg/search/search'
        })
      }
    }
	}
</script>

<style lang="scss">

      .scroll-view-container{
        display: flex;
        
        .scroll-view-right{
          flex:3;
        }
        .scroll-view-left{
          flex: 1;
          width: 350rpx;
          .left-item{
            line-height: 60rpx;
            background-color: #f7f7f7;
            text-align:center;
            font-size: 12px;
            
            // 激活项的样式
            &.active{
              background-color: #ffffff;
              position: relative;
              
              // 渲染激活再左侧的红色指示边线
              &::before{
                position: absolute;
                content: '';
                width: 3px;
                height: 30px;
                top: 50%;
                left: 0;
                transform: translateY(-50%);
                background-color: #c00000;
              }
            }
          }
        }
        
        .cate-lv2-title{
        text-align: center;
        font-weight: bold; 
         font-size: 16px;
          padding:10px 0;
        }
        
        .cate-lv3-list{
          display: flex;
          flex-wrap: wrap;
          .cate-lv3-item{
            width:33.33%;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            image{
              width: 120rpx;
              height: 120rpx;
            }
            text{
              font-size: 12px;
            }
            
          }
          
        }
      }
</style>
